/*
 * $Id: SimpleTest3.java 3 2007-12-07 01:24:30Z rasan.rasch $
 *
 * Copyright (c) 2002-2006 UC Regents
 * 
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that (i) the above copyright notices and this permission notice appear in
 * all copies of the software and related documentation, and (ii) the names
 * of the UC Regents and the University of California are not used in any
 * advertising or publicity relating to the software without the specific,
 * prior written permission of the University of California.
 * 
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY
 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY
 * THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE
 * OR PERFORMANCE OF THIS SOFTWARE.
 */

package org.cdl.noid;

import java.io.*;
import java.util.ArrayList;
import java.util.regex.*;
import junit.framework.*;

/** 
 * Equivalent to noid2.t.
 * 
 * @author Rasan Rasch (rasan@nyu.edu)
 * @version $Revision: 3 $
 */
public class SimpleTest3 extends TestCase {

	public SimpleTest3(String name) {
		super(name);
	}
	
	protected void setUp() {
	}

	public void testFoo() {

		Minter minter = new Minter();

		File noidDir = new File("dbnoid");

		// XXX what if this is a regualr file?
		if (noidDir.isDirectory()) {
			Util.deleteDir(noidDir);
		}

		Util.doExec("./runNoid.sh dbcreate tst5.rde long 13030 cdlib.org noidTest >/dev/null");

		assertTrue("dbnoid was created failed", noidDir.isDirectory());

		if (!noidDir.isDirectory()) {
			System.err.println("something is seriously wrong, stopped");
			System.exit(1);
		}

		String[] files = { "README", "log", "logbdb", "noid.bdb" };
		for (int i = 0; i < files.length; i++) {
			File f = new File("dbnoid/" + files[i]);
			assertTrue("dbnoid/" + files[i] + " was not created", f.exists());
		}

		if (!(new File("dbnoid/noid.bdb")).exists()) {
			System.err.println("something is seriously wrong, stopped");
			System.exit(1);
		}

		ArrayList<String> output = new ArrayList<String>();

		// Mint all but the last two of 290.
		Util.doExec("./runNoid.sh mint 288", output);

		String line;
		Pattern patt = Pattern.compile("^\\s*id:\\s+");
		Matcher matcher = patt.matcher("");
		for (int i = 0; i < output.size(); i++) {
			line = (String) output.get(i);
			matcher.reset(line);
			line = matcher.replaceFirst("");
			output.set(i, line);
		}

		int len = output.size();

		if (len > 0 && Util.isEmpty((String) output.get(len - 1))) {
			output.remove(len - 1);
		}

		assertEquals("number of minted noids is 288", 288, output.size());

		String[] save_noid = new String[3];
		save_noid[0] = (String) output.get(20);
		save_noid[1] = (String) output.get(55);
		save_noid[2] = (String) output.get(155);

		// XXX start from here
		Util.doExec("./runNoid.sh mint 1");

		output.clear();
		Util.doExec("./runNoid.sh queue now " + save_noid[0] + " 2>&1", output);
		
		Util.doExec("./runNoid.sh hold release "
			+ save_noid[0] + " "
			+ save_noid[1] + " "
			+ save_noid[2] + " > /dev/null");

		Util.doExec("./runNoid.sh queue now "
			+ save_noid[0] + " "
			+ save_noid[1] + " "
			+ save_noid[2] + " > /dev/null");

		Util.doExec("./runNoid.sh mint 3", output);

		for (int i = 0; i < output.size(); i++) {
			line = (String) output.get(i);
			matcher.reset(line);
			line = matcher.replaceFirst("");
			output.set(i, line);
		}

		len = output.size();

		if (len > 0 && Util.isEmpty((String) output.get(len - 1))) {
			output.remove(len - 1);
		}

		assertEquals("(minted 3 queued noids) number of minted noids is 3", 3, output.size());

		assertEquals("first of three queued & reminted noids",
			(String) output.get(0), save_noid[0]);
		assertEquals("second of three queued & reminted noids",
			(String) output.get(1), save_noid[1]);
		assertEquals("third of three queued & reminted noids",
			(String) output.get(2), save_noid[2]);

		Util.doExec("./runNoid.sh mint 1", output);
		

		
	}

}
